aboutsummaryrefslogtreecommitdiff
path: root/src/app/kdrama/[id]/buttons.jsx
blob: a7d0f607ccfb0f54440583370bc6095587990e18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use client";
import styles from "../styles/info.module.css";
import getVideoLink from "../components/videoLink";
import React, { useEffect, useState } from "react";
import { MediaPlayer, MediaProvider } from "@vidstack/react";
import "@vidstack/react/player/styles/base.css";
import "@vidstack/react/player/styles/plyr/theme.css";
import {
	PlyrLayout,
	plyrLayoutIcons,
} from "@vidstack/react/player/layouts/plyr";

export default function EpisodesButtons({ data: episodeData, id: dramaId }) {
	const [videoLink, setVideoLink] = useState(null);
	const [episode, setEpisode] = useState("");

	async function test(a, b, episodeText) {
		let link = await getVideoLink(a, b);
		setVideoLink(link);
		setEpisode(episodeText);
	}

	// Auto loads the first episode
	useEffect(() => {
		const fetchData = async () => {
			try {
				let firstVideoLink = episodeData[0].id;
				let firstLink = await getVideoLink(firstVideoLink, dramaId);
				setVideoLink(firstLink);
				setEpisode("Episode 1");
			} catch (error) {
				console.log("Some error occured", error);
				return;
			}
		};

		fetchData();
	}, []);

	return (
		<div>
			<div className={styles.EpisodesContainer}>
				<h2>Episodes</h2>
				<div className={styles.EpisodeButtons}>
					{episodeData &&
						episodeData.map((item, index) => (
							<button
								key={index}
								onClick={(event) => {
									test(item.id, dramaId, item.title);
									event.target.style.backgroundColor =
										"var(--soft-purple)";
								}}
							>
								{item.title}
							</button>
						))}
				</div>
			</div>

			<div className={styles.VideoContainer}>
				{videoLink && (
					<div className={styles.Video}>
						<MediaPlayer
							title="dramaPlayer"
							src={videoLink}
							aspectRatio="16/9"
							load="eager"
							className={styles.VideoPlayer}
							playsInline
							volume={0.5}
						>
							<MediaProvider />
							<PlyrLayout icons={plyrLayoutIcons} />
						</MediaPlayer>
						<p>{episode}</p>
					</div>
				)}
			</div>
		</div>
	);
}